﻿/*
 * Greenshot - a free and open source screenshot tool
 * Copyright (C) 2013 Robin Krom
 * 
 * For more information see: http://getgreenshot.org/
 * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 1 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Greenshot2Core.Interfaces;

namespace Greenshot2.Contract {
	[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
	public class ContractImpl : IGreenshot2Contract, IDisposable {
		private static ServiceHost host = null;
		private bool exit = false;

		private ContractImpl() {
		}

		public void Dispose() {
			host.Close();
			host = null;
		}

		public bool isRunning {
			get {
				return !exit;
			}
		}

		public static ContractImpl Instance(string sessionToken) {
			if (host == null) {
				ContractImpl instance = new ContractImpl();
				host = new ServiceHost(instance, new Uri[] { new Uri("net.pipe://localhost") });
				host.AddServiceEndpoint(typeof(IGreenshot2Contract), new NetNamedPipeBinding(), "Greenshot_" + sessionToken);
				host.Open();
			}

			return (ContractImpl)host.SingletonInstance;
		}

		public void Exit(string reason) {
			Console.WriteLine("Exit called with reason: " + reason);
			exit = true;
		}

		public void ReloadConfig() {
			Console.WriteLine("ReloadConfig called.");
		}

		public void OpenFile(string filename) {
			Console.WriteLine("OpenFile called with filename: " + filename);
		}
	}
}
